﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using snat.model;
using snat.proxy.items;

namespace algorithms.Factories.Relabel {
    public class RelabelClusterFactory : INodeFactory {

        //email -> node
        Dictionary<String, ClusterNode> nodeLookup = new Dictionary<String, ClusterNode>();

        public RelabelClusterFactory(String namesFile) {
            using (StreamReader input = new StreamReader(namesFile)) {
                int id = 0;
                while (!input.EndOfStream) {
                    String line = input.ReadLine();
                    Console.WriteLine(line);
                    String[] temp = line.Split(':');
                    String name = temp[0].Trim();
                    ClusterNode node = new ClusterNode(id++, name);
                    String[] emails = temp[1].Split(',');
                    foreach (String email in emails.Where(e => !e.Equals(String.Empty))) {
                        nodeLookup.Add(email.Trim(), node);
                    }
                }
            }
        }

        public BaseNode Instantiate(DBEntity e) {
            if (nodeLookup.ContainsKey(e.EntityName.Trim())) {
                return nodeLookup[e.EntityName];
            }
            return null;
        }

    }
}
